﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO.Ports;
using System.Windows.Forms;


namespace PPP
{
    class Communication
    {


        #region /////////////////////Fields////////////////////
        private SerialPort myserialPort = new SerialPort();
   
        //General output data type for all Events in the form of String Array
        public delegate void showData(string[] s);          
        
        //Motor Data Event              
        public event showData GotMOData;
        
        //Heart Beat Data Event       
        public event showData GotHBData;



        //Received Data Event
        public delegate void showINOUT(string s);
        public event showINOUT DisplayINOUTDATA;       

        private string completeData = String.Empty;
        public bool UAVisAlive =false;



        #endregion 




        public void MyportName(string myportName)
        {
            myserialPort.PortName = myportName;
        }

        public void MyBoudRate(int myBoudRate)
        {
            myserialPort.BaudRate = myBoudRate;
        }

        public Communication()
        {

            myserialPort.PortName = "COM10";
            myserialPort.BaudRate = 9600;
           myserialPort.NewLine = "\r";
           // myserialPort.ReadBufferSize = 20;
            myserialPort.DiscardNull = true;

            myserialPort.DataReceived += new SerialDataReceivedEventHandler(myserialPort_DataReceived);

        }

        void myserialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
              //  int btr = myserialPort.BytesToRead;
                string sheck = myserialPort.ReadLine();
               
               // System.Console.WriteLine(myserialPort.ReadExisting());
              //  DisplayINOUTDATA(sheck);
              parseData(sheck);
            }
            catch (Exception ex)
            {
                
                MessageBox.Show(ex.Message);
            }
        }

        public void parseData(string data)
        {
            //case number one We have a complete data at once
            if (data.Contains('$') == true  )
            {
                      
                completeData = String.Empty;
                completeData = data.Substring(data.IndexOf('$') + 1);
              //  completeData = completeData.Remove(completeData.IndexOf('\r'));
                dataDistributer(completeData);

              DisplayINOUTDATA(completeData);



             }
         
        }

        private void dataDistributer(string completeData)
        {
            
            string[] fields = completeData.Split(',');



            //if (fields[0] == "HB")
            //{
            //    UAVisAlive = true;


            //}
            //else
            //{
            //    UAVisAlive = false;


            //}






            switch (fields[0])
            {
                case "GPGGA": // Depth below transducer 

                    break;

                case "MO": // Depth below transducer 

                   // System.Console.WriteLine("Got Motors " + fields[1]); 
                    parseMOdata(fields);
                    break;

                case "MTW": // Water temperature 
                    //  mtw(fields);
                    break;

                case "MWV": // Wind speed and angle 
                    // mwv(fields);
                    break;

                case "HB": // Water speed and heading
                    parseHBData(fields);
                    break;

                default:
                    break;
            }

        
            completeData = String.Empty;

            
        }

        private void parseHBData(string[] splittedData)
        {
          GotHBData(splittedData);
        }
        
        public void parseMOdata(string[] splittedData)
        {
            
            GotMOData(splittedData);                  

        }

        public void StartCommuniation()
        {

            if (myserialPort.IsOpen != true)
            {
                myserialPort.Open();
            }

        }

        public void StopCommuniation()
        {

            if (myserialPort.IsOpen == true)
            {
                myserialPort.Close();
            }

        }

        public void SendDataToUAV(string s)
        {
            try
            {
                if (myserialPort.IsOpen)
                {

              //  myserialPort.Write(s);
                myserialPort.WriteLine(s);
                 // myserialPort.Write(new char[] { '\r' }, 0, 1);

                }

            }
            catch (Exception ex )
            {

                DisplayINOUTDATA(ex.Message);
            }

        }





    }
}

